package com.wenwei.parking;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import rx.Subscription;

/**
 * Created by Administrator on 2016/7/19.
 *
 * Fragment基类，封装所有Fragment的共有属性
 */
public abstract class BaseFragment extends LazyFragment {

    protected Subscription subscription;
    public FragmentActivity mActivity;

    /**
     * 此方法可以得到上下文对象
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 返回一个需要展示的View
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        View view = initView(inflater);
        return view;
    }

    /**
     * 当Activity初始化之后可以在这里进行一些数据的初始化操作
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initEvent();
        initData();
    }


    /**
     * 子类可以复写此方法初始化事件
     */
    protected  void initEvent(){
    }
    /**
     * 子类可以复写此方法进行网络数据请求
     */
    protected  void initData(){
    }

    /**
     * 子类实现此抽象方法返回View进行展示
     *
     * @return
     */
    public abstract View initView(LayoutInflater inflater);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unsubscribe();
    }

    protected void unsubscribe() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }

    public void goActivity(Context _this, Class<? extends Activity> _class){
        startActivity(new Intent(_this,_class));
    }

    public void goActivity(Context _this,Class<? extends Activity> _class,Bundle bl){
        startActivity(new Intent(_this,_class).putExtras(bl));
    }

    public void goActivity(Context _this,Class<? extends Activity> _class,Bundle bl,int requestCode){
        if(bl == null){
            startActivityForResult(new Intent(_this,_class),requestCode);
        }else{
            startActivityForResult(new Intent(_this,_class).putExtras(bl),requestCode);
        }
    }
}
